﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;

namespace DAL
{
    static public class Logueo
    {
        private static ILog _logger;

        static Logueo()
        {
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));
            _logger = LogManager.GetLogger("LogError");
        }

        public enum Nivel { normal, critico }

        public static void LogueaInfo(string Mensaje, params object[] objetos)
        {
            StringBuilder sb = new StringBuilder(Mensaje);
            sb.Append(". ");
            if (objetos.Length > 0)
            {
                sb.Append("Valores: ");
                string strSeparador = "";
                foreach (object obj in objetos)
                {
                    sb.Append(strSeparador);
                    strSeparador = " ; ";
                    if (obj is System.Collections.IList)
                    {
                        System.Collections.IList Lista = (System.Collections.IList)obj;
                        sb.AppendFormat("Tipo = {0} - CantidadElementos = {1}", obj.GetType().Name, Lista.Count);
                    }
                    else
                    {
                        if (obj == null)
                        {
                            sb.AppendFormat("Null");
                        }
                        else
                        {
                            sb.AppendFormat("Tipo = {0} - ToString() = {1}", obj.GetType().Name, obj.ToString());
                        }
                    }
                }
            }
            _logger.Info(sb.ToString());
        }

        public static void LogueaError(Exception Error, params object[] objetos)
        {
            LogueaError(Error, Nivel.normal, objetos);
        }

        public static void LogueaError(Exception Error, Nivel Nivel, params object[] objetos)
        {
            if (Error is System.Threading.ThreadAbortException)
                return;
            StringBuilder sb = new StringBuilder();
            sb.Append(". ");
            if (objetos.Length > 0)
            {
                sb.Append("Valores: ");
                string strSeparador = "";
                foreach (object obj in objetos)
                {
                    sb.Append(strSeparador);
                    strSeparador = " ; ";
                    if (obj is System.Collections.IList)
                    {
                        System.Collections.IList Lista = (System.Collections.IList)obj;
                        sb.AppendFormat("Tipo = {0} - CantidadElementos = {1}", obj.GetType().Name, Lista.Count);
                    }
                    else
                    {
                        sb.AppendFormat("Tipo = {0} - ToString() = {1}", obj.GetType().Name, obj.ToString());
                    }
                }
            }
            _logger.Error(sb.ToString(), Error);
        }
    }
}
